package com.principalservice.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.principalservice.mapper.UserInfoMapper;
import generator.domain.Holiday;
import com.principalservice.service.HolidayService;
import com.principalservice.mapper.HolidayMapper;
import generator.domain.UserInfo;
import generator.support.ResponseResult;
import generator.vo.likeHoliday;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.jws.soap.SOAPBinding;
import java.text.SimpleDateFormat;
import java.util.*;

/**
* @author 杨健康
* @description 针对表【t_holiday】的数据库操作Service实现
* @createDate 2022-06-18 12:42:40
*/
@Service
public class HolidayServiceImpl extends ServiceImpl<HolidayMapper, Holiday>
    implements HolidayService{

    @Autowired
    HolidayMapper holidayMapper;

    @Autowired
    UserInfoMapper userInfoMapper;

    @Override
    public List<Map<String, Object>> getAllHoliday() {
        List<Map<String, Object>> result = new ArrayList<>();
        QueryWrapper<Holiday> holidayqw = new QueryWrapper<>();
        List<Holiday> holidays = holidayMapper.selectList(holidayqw);
        for (Holiday holiday : holidays) {
            Map<String, Object> map = new HashMap<>();
            map.put("Holiday", holiday);
            QueryWrapper<UserInfo> userqw = new QueryWrapper<>();
            userqw.eq("user_info_id", holiday.getHolidayUserId());
            List<UserInfo> userInfos = userInfoMapper.selectList(userqw);
            if (userInfos.isEmpty()){
                UserInfo user = new UserInfo();
                user.setUserName("该用户已被删除");
                map.put("UserInfo", user);
            } else {
                map.put("UserInfo", userInfos.get(0));
            }
            result.add(map);
        }
        return result;
    }

    @Override
    public List<Map<String, Object>> likeHoliday(likeHoliday likeHoliday) {
        Holiday holiday = likeHoliday.getHoliday();
        UserInfo userInfo = likeHoliday.getUserInfo();

        // 构建筛选条件
        QueryWrapper<Holiday> holidayqw = new QueryWrapper<>();
        if (!("".equals(userInfo.getUserName()) || userInfo.getUserName() == null)){
            QueryWrapper<UserInfo> userqw = new QueryWrapper<>();
            userqw.eq("user_name", userInfo.getUserName());
            List<UserInfo> userInfos = userInfoMapper.selectList(userqw);
            if (!userInfos.isEmpty()){
                holidayqw.eq("holiday_user_id", userInfos.get(0).getUserInfoId());
            }
        }
        if (!("".equals(holiday.getHolidayType())) || holiday.getHolidayType() == null){
            holidayqw.eq("holiday_type", holiday.getHolidayType());
        }
        if (!("".equals(holiday.getHolidayStatus())) || holiday.getHolidayStatus() == null){
            holidayqw.eq("holiday_status", holiday.getHolidayStatus());
        }

        // 查询
        List<Holiday> holidays = holidayMapper.selectList(holidayqw);
        List<Map<String, Object>> result = new ArrayList<>();
        for (Holiday holiday1 : holidays) {
            Map<String, Object> map = new HashMap<>();
            map.put("Holiday", holiday1);
            QueryWrapper<UserInfo> userqw = new QueryWrapper<>();
            userqw.eq("user_info_id", holiday1.getHolidayUserId());
            List<UserInfo> userInfos = userInfoMapper.selectList(userqw);
            if (userInfos.isEmpty()){
                UserInfo user = new UserInfo();
                user.setUserName("该用户已被删除");
                map.put("UserInfo", user);
            } else {
                map.put("UserInfo", userInfos.get(0));
            }
            result.add(map);
        }
        return result;
    }

    @Override
    public ResponseResult<Object> addHoliday(likeHoliday likeHoliday) {
        Holiday holiday = likeHoliday.getHoliday();
        UserInfo user = likeHoliday.getUserInfo();
        // 获取user的id
        QueryWrapper<UserInfo> userqw = new QueryWrapper<>();
        userqw.eq("user_name", user.getUserName());
        List<UserInfo> userInfos = userInfoMapper.selectList(userqw);
        if (userInfos.isEmpty()){
            return new ResponseResult<>(400, "没有此员工");
        }
        holiday.setHolidayUserId(userInfos.get(0).getUserInfoId());
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        holiday.setHolidayCreateTime(format.format(new Date()));
        // 查看是否有编号冲突
        QueryWrapper<Holiday> holidayqw = new QueryWrapper<>();
        holidayqw.eq("holiday_no", holiday.getHolidayNo());
        List<Holiday> holidays = holidayMapper.selectList(holidayqw);
        if (!holidays.isEmpty()){
            holiday.setHolidayNo(UUID.randomUUID().toString());
        }
        if (holidayMapper.insert(holiday) > 0){
            return new ResponseResult<>();
        }
        return new ResponseResult<>(400, "添加失败");
    }

    @Override
    public ResponseResult<Object> editHoli(likeHoliday likeHoliday) {
        Holiday holiday = likeHoliday.getHoliday();
        UserInfo userInfo = likeHoliday.getUserInfo();
        // 查看是否有编号冲突
        QueryWrapper<Holiday> holidayqw = new QueryWrapper<>();
        holidayqw.eq("holiday_no", holiday.getHolidayNo());
        List<Holiday> holidays = holidayMapper.selectList(holidayqw);
        if (!holidays.isEmpty()){
            holiday.setHolidayNo(UUID.randomUUID().toString());
        }

        // 获取user的id
        QueryWrapper<UserInfo> userqw = new QueryWrapper<>();
        userqw.eq("user_name", userInfo.getUserName());
        List<UserInfo> userInfos = userInfoMapper.selectList(userqw);
        if (userInfos.isEmpty()){
            return new ResponseResult<>(400, "没有此员工");
        }
        holiday.setHolidayUserId(userInfos.get(0).getUserInfoId());

        if (holidayMapper.updateById(holiday) > 0){
            return new ResponseResult<>();
        }
        return new ResponseResult<>(400, "修改失败");
    }
}




